package com.gxa.modules.service.propertyservice.Impl;

import com.gxa.common.R;
import com.gxa.modules.mapper.propertymapper.CarManageMapper;
import com.gxa.modules.pojo.Car;
import com.gxa.modules.pojo.Visitor;
import com.gxa.modules.service.propertyservice.CarManageService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;
import java.util.HashMap;
import java.util.List;

@Service
public class CarManageImplService implements CarManageService {

    @Resource
    private CarManageMapper carManageMapper;

    @Override
    public R selectAllCarManageForPage(int page, int size) {
        //分页
        int index=(page-1)*size;
        R r = new R();
        HashMap<Object, Object> map = new HashMap<>();
        List<Car> cars = carManageMapper.selectAllCarManageForPage(index,size);
        map.put("data", cars);
        int res = carManageMapper.selectAllCarManageForPagesum( page,size);
        map.put("count",res);
        if(res > 0)
        {
            r.setCode(200);
            r.setMsg("查询成功");
            r.setData(map);
        }

        return r;
    }

    @Override
    public R selectAllCarManage(int page, int size, String plateNumber, String carModel, String phone) {
        int index=(page-1)*size;
        R r = new R();
        HashMap<Object, Object> map = new HashMap<>();
        List<Car> cars = carManageMapper.selectAllCarManage(index,size,
                plateNumber,carModel,phone);

        map.put("data",cars);


        int res = carManageMapper.selectAllCarManagesum( plateNumber,carModel,phone);
        map.put("count",res);
        if(res > 0)
        {
            r.setCode(200);
            r.setMsg("查询成功");
            r.setData(map);
        }


        return r;
    }

    @Override
    public R insertCarManage(Car car) {
        R r = new R();
        int res = carManageMapper.insertCarManage(car);
        if(res > 0)
        {
            r.setCode(200);
            r.setMsg("新增成功");
        }
        return r;
    }

    @Override
    public R deleteCarManageById(String id) {
        R<Object> r = new R<>();

        if ( id ==null || "".equals(id) ){
            r.setCode(500);
            r.setMsg("请选择要删除的信息！");
            return r;
        }

        r.setCode(500);
        r.setMsg("删除失败！");

        if(carManageMapper.deleteCarManageById(id)>0){
            r.setCode(200);
            r.setMsg("删除成功！");
        }

        return r;
    }

    @Override
    public void deleteCarManage(List<Integer> list) {
        carManageMapper.deleteCarManage(list);
    }

    @Override
    public R selectDetails(int id) {
        R r = new R();
        List<Car> cars = carManageMapper.selectDetails(id);
        if (cars != null){
            r.setCode(200);
            r.setMsg("查询成功！");
            r.setData(cars);
        }
        return r;

    }

    @Override
    public R selectAllCarRecordForPage(int page, int size) {
        //分页
        int index=(page-1)*size;
        R r = new R();
        HashMap<Object, Object> map = new HashMap<>();
        List<Car> cars = carManageMapper.selectAllCarRecordForPage(index,size);
        map.put("data",cars);
        int res = carManageMapper.selectAllCarRecordForPagesum( page,size);
        map.put("count",res);
        if(res > 0)
        {
            r.setCode(200);
            r.setMsg("查询成功");
            r.setData(map);
        }
        return r;
    }

    @Override
    public R selectAllCarRecord(int page, int size, String plateNumber, Date accessTime) {
        int index=(page-1)*size;
        R r = new R();
        HashMap<Object, Object> map = new HashMap<>();
        List<Car> cars = carManageMapper.selectAllCarRecord(index,size,
                plateNumber,accessTime);

        map.put("data",cars);


        int res = carManageMapper.selectAllCarRecordsum( plateNumber,accessTime);
        map.put("count",res);
        if(res > 0)
        {
            r.setCode(200);
            r.setMsg("查询成功");
            r.setData(map);
        }
        return r;
    }

    @Override
    public R deleteCarRecordById(String id) {
        R<Object> r = new R<>();

        if ( id ==null || "".equals(id) ){
            r.setCode(500);
            r.setMsg("请选择要删除的信息！");
            return r;
        }

        r.setCode(500);
        r.setMsg("删除失败！");

        if(carManageMapper.deleteCarRecordById(id)>0){
            r.setCode(200);
            r.setMsg("删除成功！");
        }

        return r;
    }



}
